package com.shuqi.activity.adapter;

import java.util.List;

import android.content.Context;
import android.graphics.Color;
import android.text.TextPaint;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.shuqi.common.utils.SqImageManager;
import com.shuqi.controller.R;
import com.shuqi.database.model.BookInfo;

public class RecommendListAdapter extends BaseAdapter {

    private LayoutInflater inflater;
    private List<BookInfo> list;
    private static final int ITEMVIEW_CONTENT = 1;
    private static final int ITEMVIEW_TITEL = 2;
    private static final int ITEMVIEW_MORE = 3;
    private static final int ITEMVIEW_CONUT = 4;

    public RecommendListAdapter(Context context) {
        inflater = LayoutInflater.from(context);
    }

    public List<BookInfo> getData() {
        return list;
    }

    public void setData(List<BookInfo> list) {
        this.list = list;
    }

    @Override
    public int getCount() {
        if (list == null || list.size() <= 0) {
            return 0;
        } else {
            return list.size();
        }
    }

    @Override
    public int getItemViewType(int position) {
        if (list == null) {
            return 0;
        }
        BookInfo info = list.get(position);
        if (info != null) {
            if (TextUtils.isEmpty(info.getBookId())) {
                if (info.getBookName().endsWith("查看更多书籍")) {
                    return ITEMVIEW_MORE;
                } else {
                    return ITEMVIEW_TITEL;
                }
            } else {
                return ITEMVIEW_CONTENT;
            }
        }

        return 0;
    }

    @Override
    public int getViewTypeCount() {
        // TODO Auto-generated method stub
        return ITEMVIEW_CONUT;
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        View view = null;
        Holder holder = null;
        int type = getItemViewType(position);
        if (convertView != null) {
            view = convertView;
            holder = (Holder) view.getTag();
        } else {
            holder = new Holder();
            switch (type) {
                case ITEMVIEW_MORE:
                    view = inflater.inflate(R.layout.item_recommend_layout_more, parent, false);
                    holder.more = (TextView) view.findViewById(R.id.item_more);
                    break;
                case ITEMVIEW_TITEL:
                    view = inflater.inflate(R.layout.item_recommend_layout_title, parent, false);
                    holder.title = (TextView) view.findViewById(R.id.item_title);
//                    holder.item_icon = (TextView) view.findViewById(R.id.item_icon);
                    holder.line = (View)view.findViewById(R.id.line);
                    break;
                case ITEMVIEW_CONTENT:
                    view = inflater.inflate(R.layout.item_recommend_list, parent, false);
                    holder.ranklist_title_image =
                            (ImageView) view.findViewById(R.id.ranklist_title_image);
                    holder.ranklist_bookname_text =
                            (TextView) view.findViewById(R.id.ranklist_bookname_text);
                    holder.ranklist_type_text =
                            (TextView) view.findViewById(R.id.ranklist_type_text);
                    holder.ranklist_description_text =
                            (TextView) view.findViewById(R.id.ranklist_description_text);
                    holder.ranklist_author_text =
                            (TextView) view.findViewById(R.id.ranklist_author_text);

                    view.setTag(holder);
                    break;
                default:
                    break;
            }
            view.setTag(holder);

        }
        final BookInfo info = list.get(position);
        if (info != null) {
            if (TextUtils.isEmpty(info.getBookId())) {
                if (info.getBookName().endsWith("查看更多书籍")) {
                    holder.more.setText("查看更多书籍");
                } else {
                    if (info.getBookStatus() != null) {
                        holder.title.setText(info.getBookName());
                        holder.title.setTextColor(Color.parseColor(info.getBookStatus()));
//                        holder.item_icon.setBackgroundColor(Color.parseColor(info.getBookStatus()));
                        holder.line.setBackgroundColor(Color.parseColor(info.getBookStatus()));
                    }
                }
            } else {
                holder.ranklist_bookname_text.setText(info.getBookName());
                holder.ranklist_type_text.setText(info.getBookType());
                // 设置类型颜色
                if (!TextUtils.isEmpty(info.getBookStatus())) {
                    // holder.ranklist_type_text.setBackgroundColor(Color.parseColor(info
                    // .getBookStatus()));
                    holder.ranklist_type_text.setTextColor(Color.parseColor(info.getBookStatus()));
                }
                holder.ranklist_description_text.setText(info.getBookIntro());
                holder.ranklist_author_text.setText(info.getBookAuthorName());
                holder.ranklist_title_image.setTag(position + "");
                SqImageManager.getImageManager().displayImage(info.getBookCoverImgUrl(),
                        holder.ranklist_title_image, SqImageManager.IMG_DEFAULT);
            }
        }

        return view;
    }

    @Override
    public boolean isEnabled(int position) {

        return super.isEnabled(position);
    }

    private static class Holder {
        public ImageView ranklist_title_image;
        public TextView ranklist_bookname_text;
        public TextView ranklist_type_text;
        public TextView title; // 标题
        public TextView more;
        public TextView ranklist_description_text;
        public TextView ranklist_author_text;
//        public TextView item_icon;
        public View line;
    }
}
